package stack.era.presentation;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;

import stack.era.domain.TimePassageController;
import stack.era.presentation.frame.ERAFrame;

public class ERAWaitingPopup extends JDialog implements Runnable {

	private static final long serialVersionUID = 2346175463362403452L;

	private int hoursToWait, currentHours;
	private ERAWaitingProgressBar progressBar;
	private ERAFrameCardPanel callback;

	public ERAWaitingPopup(ERAFrameCardPanel callback, String title,
			int hoursToWait) {
		super(ERAFrame.getInstance(), title, true);

		this.setLocation(ERAFrame.getInstance().getX() + 250, ERAFrame
				.getInstance().getY() + 250);
		this.setSize(300, 100);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

		this.callback = callback;
		this.hoursToWait = hoursToWait;
		this.currentHours = 0;

		this.progressBar = new ERAWaitingProgressBar(this.hoursToWait);

		JPanel content = new JPanel();
		content.setLayout(new BorderLayout());
		content.setBorder(BorderFactory.createLineBorder(Color.GRAY, 10));
		content.add(progressBar);

		this.setLayout(new BorderLayout());
		this.add(content, BorderLayout.CENTER);
	}

	@Override
	public void run() {
		while (this.currentHours < this.hoursToWait) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			TimePassageController.getInstance().hourPassage();
			this.currentHours++;
			this.progressBar.setValue(this.currentHours);
			// this.progressBar.repaint();
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.setVisible(false);
		this.dispose();
		this.callback.doneWaiting();
	}

	public void open() {
		new Thread(this).start();
		this.setVisible(true);
	}

}
